package patrol.mobile.com.mobilepatrol.activity.track;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.lzy.imagepicker.ui.ImagePreviewDelActivity;
import com.lzy.imagepicker.view.CropImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.realm.Realm;
import patrol.mobile.com.mobilepatrol.PatrolApp;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.BaseActivity;
import patrol.mobile.com.mobilepatrol.adapter.GlideImageLoader;
import patrol.mobile.com.mobilepatrol.adapter.ImagePickerAdapter;
import patrol.mobile.com.mobilepatrol.bean.PatrolDetailInfoBean;
import patrol.mobile.com.mobilepatrol.bean.PatrolInfoBean;
import patrol.mobile.com.mobilepatrol.manager.TrackManager;
import patrol.mobile.com.mobilepatrol.utils.CommonUtil;
import patrol.mobile.com.mobilepatrol.utils.DateUtil;
import patrol.mobile.com.mobilepatrol.widget.dialog.SelectDialog;

import static patrol.mobile.com.mobilepatrol.R.array.damageType;
import static patrol.mobile.com.mobilepatrol.R.array.roadWay;
import static patrol.mobile.com.mobilepatrol.R.id.et;
import static patrol.mobile.com.mobilepatrol.R.id.tv;

/**
 * Created by apple on 17/11/7.
 */

public class TrackDetailActivity extends BaseActivity implements ImagePickerAdapter.OnRecyclerViewItemClickListener {
    private ArrayList<ImageItem> selImageList;
    private ImagePickerAdapter adapter;
    public static final int IMAGE_ITEM_ADD = -1;
    public static final int REQUEST_CODE_SELECT = 100;
    public static final int REQUEST_CODE_PREVIEW = 101;
    @BindView(R.id.item1)
    RelativeLayout rl1;

    @BindView(R.id.item2)
    RelativeLayout rl2;

    @BindView(R.id.item3)
    RelativeLayout rl3;

    @BindView(R.id.item4)
    RelativeLayout rl4;

    @BindView(R.id.item5)
    RelativeLayout rl5;

    @BindView(R.id.item6)
    RelativeLayout rl6;

    @BindView(R.id.item7)
    RelativeLayout rl7;

    @BindView(R.id.item8)
    RelativeLayout rl8;

    @BindView(R.id.item9)
    RelativeLayout rl9;

    @BindView(R.id.item11)
    RelativeLayout rl11;

    @BindView(R.id.item12)
    RelativeLayout rl12;

    @BindView(R.id.item13)
    LinearLayout ll13;

    @BindView(R.id.item14)
    RelativeLayout rl14;

    @BindView(R.id.item15)
    RelativeLayout rl15;

    @BindView(R.id.rl16)
    RelativeLayout rl16;

    @BindView(R.id.rg1)
    RadioGroup rg1;

    @BindView(R.id.rb1)
    RadioButton rb1;

    @BindView(R.id.rb2)
    RadioButton rb2;

    @BindView(R.id.rb3)
    RadioButton rb3;

    @BindView(R.id.rb4)
    RadioButton rb4;

    @BindView(R.id.rb9)
    RadioButton rb9;

    @BindView(R.id.rb10)
    RadioButton rb10;

    @BindView(R.id.rg2)
    RadioGroup rg2;

    @BindView(R.id.rg3)
    RadioGroup rg3;

    @BindView(R.id.sure)
    Button bt_sure;

    private TextView tv_date;
    private TextView tv_weather;
    private TextView et_roader;
    private EditText et_sm;
    private EditText et_skm;
    private EditText et_question;
    private TextView tv_patrolType;
    private TextView tv_roadCar;
    private TextView tv_roadLine;
    private EditText et_em;
    private EditText et_ekm;
    private String patrolID;

    private String init_date;
    private String init_patrolType;
    private String init_weather;
    private String init_roader;
    private String init_car;
    private String init_line;
    private int init_sm;
    private String init_damageType;
    private String init_context;

    private int init_skm;
    private int init_em;
    private int init_ekm;

    private String init_roadAway;
    //损坏部位
    private List<String> damageTypeData = new ArrayList<>();
    private List<String> qData1 = new ArrayList<>();
    private List<String> qData2 = new ArrayList<>();
    private List<String> qData3 = new ArrayList<>();
    private List<String> roadWayData = new ArrayList<>();
    private List<String> unitData = new ArrayList<>();
    private PopupWindow mPopup;
    private TextView tv_damageType;
    private TextView tv_question;
    private TextView tv_roadWay;

    private boolean isAlter;
    String sel_warnColor = "";
    private RelativeLayout rl_roadway;
    private EditText et_potMeter;
    private EditText et_pot;
    private EditText et_endpotMeter;
    private EditText et_endpotKm;
    private String init_unit;
    private EditText et_dealInfo;
    private EditText et_workload;
    private RelativeLayout rl_workUnit;
    private TextView tv_workUnit;
    //预判级别
    private String sel_warnType;
    private String m_deal="";
    private PatrolInfoBean mPatrol;
    private String mID;
    private int init_potMeter;
    private int init_pot;
    private int init_endpotMeter;
    private int init_endpotKm;
    private String init_workload;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_track_detail;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initImagePicker();
    }

    @Override
    public void setListener() {
        bt_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isAlter){
                    alter();
                }else{
                    insert();
                }
            }
        });
        rg3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb9:
                        m_deal = "现场处理";
                        break;
                    case R.id.rb10:
                        m_deal = "计划作业";
                        break;
                }
            }
        });
        rl_workUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSelectPopup(unitData,tv_workUnit);
                if (mPopup != null && !mPopup.isShowing()) {
                    mPopup.showAsDropDown(tv_workUnit, 0, 10);
                }
            }
        });
        rl_roadway.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initSelectPopup(roadWayData,tv_roadWay);
                if (mPopup != null && !mPopup.isShowing()) {
                    mPopup.showAsDropDown(tv_roadWay, 0, 10);
                }
            }
        });
        rl9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                initSelectPopup(damageTypeData,tv_damageType);
                if (mPopup != null && !mPopup.isShowing()) {
                    mPopup.showAsDropDown(tv_damageType, 0, 10);
                }
            }
        });
        rl11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (sel_warnType){
                    case "一":
                        initSelectPopup(qData1,tv_question);
                        if (mPopup != null && !mPopup.isShowing()) {
                            mPopup.showAsDropDown(tv_question, 0, 10);
                        }
                        rg2.setVisibility(View.GONE);
                        break;
                    case "二":
                        initSelectPopup(qData2,tv_question);
                        if (mPopup != null && !mPopup.isShowing()) {
                            mPopup.showAsDropDown(tv_question, 0, 10);
                        }
                        rg2.setVisibility(View.GONE);
                        break;
                    case "三":
                        initSelectPopup(qData3,tv_question);
                        if (mPopup != null && !mPopup.isShowing()) {
                            mPopup.showAsDropDown(tv_question, 0, 10);
                        }
                        rg2.setVisibility(View.VISIBLE);
                        break;
                    case "无异常":
                        rg2.setVisibility(View.GONE);
                        break;

                }
            }
        });
        //预判级别
        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        initSelectPopup(qData1,tv_question);
                        tv_question.setText(qData1.get(0));
//                        if (mPopup != null && !mPopup.isShowing()) {
//                            mPopup.showAsDropDown(tv_question, 0, 10);
//                        }
                        rg2.setVisibility(View.GONE);
                        rg3.setVisibility(View.VISIBLE);
                        rb10.setVisibility(View.VISIBLE);
                        rb9.setVisibility(View.VISIBLE);
                        rl15.setVisibility(View.GONE);
                        rl16.setVisibility(View.VISIBLE);
                        sel_warnType = "一";
                        sel_warnColor = "";
                        m_deal = "现场处理";
                        rb9.setChecked(true);
                        rb10.setChecked(false);
                        break;
                    case R.id.rb2:
                        initSelectPopup(qData2,tv_question);
//                        if (mPopup != null && !mPopup.isShowing()) {
//                            mPopup.showAsDropDown(tv_question, 0, 10);
//                        }
                        tv_question.setText(qData2.get(0));
                        rg2.setVisibility(View.GONE);
                        rg3.setVisibility(View.VISIBLE);
                        rb10.setVisibility(View.VISIBLE);
                        rb10.setChecked(true);
                        rl15.setVisibility(View.GONE);
                        rl16.setVisibility(View.VISIBLE);
                        rb9.setVisibility(View.GONE);
                        sel_warnType = "二";
                        sel_warnColor = "";
                        m_deal = "计划作业";
                        break;
                    case R.id.rb3:
                        initSelectPopup(qData3,tv_question);
//                        if (mPopup != null && !mPopup.isShowing()) {
//                            mPopup.showAsDropDown(tv_question, 0, 10);
//                        }
                        tv_question.setText(qData3.get(0));
                        rg2.setVisibility(View.VISIBLE);
                        rl16.setVisibility(View.GONE);
                        rl15.setVisibility(View.VISIBLE);
                        m_deal = "";
                        sel_warnType = "三";
                        sel_warnColor = "红色预警";
                        break;
                    case R.id.rb4:
                        tv_question.setText("");
                        rg2.setVisibility(View.GONE);
                        rg3.setVisibility(View.VISIBLE);
                        rb10.setVisibility(View.VISIBLE);
                        rb9.setVisibility(View.VISIBLE);
                        rl15.setVisibility(View.GONE);
                        rl16.setVisibility(View.VISIBLE);
                        sel_warnType = "无异常";
                        sel_warnColor = "";
                        m_deal = "现场处理";
                        rb9.setChecked(true);
                        rb10.setChecked(false);
                        break;
                }
            }
        });
        rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId){
                    case R.id.rb1:
                        sel_warnColor = "红色预警";
                        break;
                    case R.id.rb2:
                        sel_warnColor = "橙色预警";
                        break;
                    case R.id.rb3:
                        sel_warnColor = "黄色预警";
                        break;
                    case R.id.rb4:
                        sel_warnColor = "蓝色预警";
                        break;
                }
            }
        });
    }

    private void insert() {
        final String damageType = CommonUtil.getText(tv_damageType);
        final String  question = CommonUtil.getText(tv_question);
        final String  etquestion = CommonUtil.getText(et_question);
        final String  roadWay = CommonUtil.getText(tv_roadWay);
        final String  potMeter = CommonUtil.getText(et_potMeter);
        final String  pot = CommonUtil.getText(et_pot);
        final String  endpotMeter = CommonUtil.getText(et_endpotMeter);
        final String  endpotKm = CommonUtil.getText(et_endpotKm);
        final String  workload = CommonUtil.getText(et_workload);
        final String  workUnit = CommonUtil.getText(tv_workUnit);
        final String dealinfo =CommonUtil.getText(et_dealInfo);
        switch (sel_warnType){
            case "一":
            case "二":
            case "三":
                if (damageType.equals("")){
                    showToast("请选择损坏部位");
                    return;
                }
                if (question.equals("")){

                    showToast("请填写问题描述");
                    return;
                }
                if("手动输入".equals(question)&&etquestion.equals("")){
                    showToast("请填写问题描述");
                    return;
                }
                if (potMeter.equals("")){

                    showToast("请选桩号");
                    return;
                }
                if (pot.equals("")){

                    showToast("请选桩号");
                    return;
                }
                if (endpotMeter.equals("")){

                    showToast("请选桩号");
                    return;
                }
                if (endpotKm.equals("")){

                    showToast("请选桩号");
                    return;
                }
                break;
            case "无异常":
                break;
        }

        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                PatrolDetailInfoBean p = realm.createObject(PatrolDetailInfoBean.class);
                p.ID = CommonUtil.getUUID();
                p.patrolID = patrolID;
                p.checkDate = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm");
                p.updatetime = System.currentTimeMillis();
                p.damageType = damageType;
                p.warnType = sel_warnType;
                p.warnColor = sel_warnColor;
                if("手动输入".equals(question)){
                    p.context=etquestion;
                }else{
                    p.context = question;
                }
                p.roadWay = roadWay;
                p.potMeter = Integer.valueOf(potMeter);
                p.pot = Integer.valueOf(pot);
                p.endpotMeter = Integer.valueOf(endpotMeter);
                p.endpotKm = Integer.valueOf(endpotKm);
                p.workload =workload;
                p.workUnit=workUnit;
                p.dealInfo = dealinfo;
                p.deal = m_deal;
//                p.photoNumber = "";
                p.statu = 0;
                mPatrol.detailInfoBeans.add(p);
                mPatrol.status=0;

                if (selImageList!=null&&selImageList.size()>0){
                    for (int i=0;i<selImageList.size();i++){
                        p.photoNumber = p.photoNumber+selImageList.get(i).path+",";
                    }

                }
            }

        });
        showToast("添加成功");
        CommonUtil.exitActivityAndBackAnim(TrackDetailActivity.this,true);
    }

    private void alter() {
        final String damageType = CommonUtil.getText(tv_damageType);
        final String  question = CommonUtil.getText(tv_question);
        final String  etquestion = CommonUtil.getText(et_question);
        final String  roadWay = CommonUtil.getText(tv_roadWay);
        final String  potMeter = CommonUtil.getText(et_potMeter);
        final String  pot = CommonUtil.getText(et_pot);
        final String  endpotMeter = CommonUtil.getText(et_endpotMeter);
        final String  endpotKm = CommonUtil.getText(et_endpotKm);
        final String  workload = CommonUtil.getText(et_workload);
        final String  workUnit = CommonUtil.getText(tv_workUnit);
        final String dealinfo =CommonUtil.getText(et_dealInfo);
        switch (sel_warnType){
            case "一":
            case "二":
            case "三":
                if (damageType.equals("")){
                    showToast("请选择损坏部位");
                    return;
                }
                if (question.equals("")){

                    showToast("请填写问题描述");
                    return;
                }
                if("手动输入".equals(question)&&etquestion.equals("")){
                    showToast("请填写问题描述");
                    return;
                }
                if (potMeter.equals("")){

                    showToast("请选桩号");
                    return;
                }
                if (pot.equals("")){

                    showToast("请选桩号");
                    return;
                }
                if (endpotMeter.equals("")){

                    showToast("请选桩号");
                    return;
                }
                if (endpotKm.equals("")){

                    showToast("请选桩号");
                    return;
                }
                break;
            case "无异常":
                break;
        }
        PatrolApp.realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                PatrolDetailInfoBean p = realm.where(PatrolDetailInfoBean.class).equalTo("ID",mID).findFirst();
                p.ID = mID;
                p.patrolID = patrolID;
                p.damageType = damageType;
                p.warnType = sel_warnType;
                p.warnColor = sel_warnColor;
                if("手动输入".equals(question)){
                    p.context=etquestion;
                }else{
                    p.context = question;
                }
                p.roadWay = roadWay;
                p.potMeter = Integer.valueOf(potMeter);
                p.pot = Integer.valueOf(pot);
                p.endpotMeter = Integer.valueOf(endpotMeter);
                p.endpotKm = Integer.valueOf(endpotKm);
                p.workload =workload;
                p.workUnit=workUnit;
                p.dealInfo = dealinfo;
                p.deal = m_deal;
//                p.photoNumber = "";
                p.updatetime = System.currentTimeMillis();
                p.checkDate = DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss");
                p.statu = 0;
                mPatrol.status=0;
                    if (selImageList!=null&&selImageList.size()>0){
                        for (int i=0;i<selImageList.size();i++){
                            p.photoNumber += selImageList.get(i).path+",";
                        }
                    }

            }

        });
        showToast("修改成功");
        CommonUtil.exitActivityAndBackAnim(TrackDetailActivity.this,true);
    }


    @Override
    public void initTitle() {
        iv_back.setImageResource(R.mipmap.up_white);
        iv_back.setVisibility(View.VISIBLE);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CommonUtil.exitActivityAndBackAnim(TrackDetailActivity.this,true);
            }
        });
        tv_center_title.setVisibility(View.VISIBLE);
        tv_center_title.setText("巡道管理明细");
    }
    @Override
    public void initView() {
        super.initView();
        selImageList = new ArrayList<>();
        adapter = new ImagePickerAdapter(this, selImageList, 9);
        adapter.setOnItemClickListener(this);
        RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 4));
        recyclerView.setHasFixedSize(true);
        recyclerView.setAdapter(adapter);
        iniData();
        TextView tv1 = rl1.findViewById(tv);
        tv1.setText("检查日期:");
        tv_date = rl1.findViewById(R.id.tv1);
        tv_date.setText(init_date);

        TextView tv2 = rl2.findViewById(tv);
        tv2.setText("巡查类型");
        tv_patrolType = rl2.findViewById(R.id.tv1);
        tv_patrolType.setText(init_patrolType);

        TextView tv3 = rl3.findViewById(tv);
        tv3.setText("当日天气：");
        tv_weather = rl3.findViewById(R.id.tv1);
        tv_weather.setText(init_weather);

        TextView tv4 = rl4.findViewById(tv);
        tv4.setText("巡查人员：");
        et_roader = rl4.findViewById(R.id.tv1);
        et_roader.setText(init_roader);

        TextView tv5 = rl5.findViewById(tv);
        tv5.setText("巡查车辆：");
        tv_roadCar = rl5.findViewById(R.id.tv1);
        tv_roadCar.setText(init_car);

        TextView tv6 = rl6.findViewById(tv);
        tv6.setText("巡查路线：");
        tv_roadLine = rl6.findViewById(R.id.tv1);
        tv_roadLine.setText(init_line);


        TextView tv7 = rl7.findViewById(R.id.tv1);
        tv7.setText("起始桩号：");
        et_skm = rl7.findViewById(R.id.et1);
        et_sm = rl7.findViewById(R.id.et2);
        et_skm.setText(init_skm+"");
        et_sm.setText(String.valueOf(init_sm));

        TextView tv8 = rl8.findViewById(R.id.tv1);
        tv8.setText("终点桩号：");
        et_ekm = rl8.findViewById(R.id.et1);
        et_em = rl8.findViewById(R.id.et2);
        et_ekm.setText(init_ekm+"");
        et_em.setText(String.valueOf(init_em));

        TextView tv9 = rl9.findViewById(tv);
        tv9.setText("损坏部位");
        tv_damageType = rl9.findViewById(R.id.tv1);
        tv_damageType.setText(init_damageType);

        TextView tv11 = rl11.findViewById(tv);
        tv11.setText("发现问题情况描述：");
        tv_question = rl11.findViewById(R.id.tv1);
        tv_question.setText(init_context);

        TextView tv12 = rl12.findViewById(tv);
        tv12.setText("发现问题情况描述：");
        et_question = rl12.findViewById(et);
        et_question.setText("");

        //
        rl_roadway = ll13.findViewById(R.id.rl_roadway);
        tv_roadWay = ll13.findViewById(R.id.tv_roadWay);
        tv_roadWay.setText(init_roadAway);
        et_potMeter = ll13.findViewById(R.id.et2);
        et_potMeter.setText(init_potMeter+"");
        et_pot = ll13.findViewById(R.id.et1);
        et_pot.setText(init_pot+"");

        et_endpotMeter = ll13.findViewById(R.id.et2_2);
        et_endpotMeter.setText(init_endpotMeter+"");
        et_endpotKm = ll13.findViewById(R.id.et1_1);
        et_endpotKm.setText(init_endpotKm+"");
        //14
        et_workload = rl14.findViewById(R.id.et);
        rl_workUnit = rl14.findViewById(R.id.rl_workUnit);
        tv_workUnit = rl14.findViewById(R.id.tv1);
        tv_workUnit.setText(init_unit);

        //15
        et_dealInfo = rl15.findViewById(R.id.et);
        et_workload.setText(init_workload);



        switch (sel_warnType){
            case "一":
                rl15.setVisibility(View.GONE);
                rl16.setVisibility(View.VISIBLE);
                rb10.setVisibility(View.VISIBLE);
                rb9.setVisibility(View.VISIBLE);
                rb1.setChecked(true);
                initSelectPopup(qData1,tv_question);
                break;
            case "二":
                rl15.setVisibility(View.GONE);
                rl16.setVisibility(View.VISIBLE);
                rb10.setVisibility(View.VISIBLE);
                rb10.setChecked(true);
                rb9.setVisibility(View.GONE);
                rb2.setChecked(true);
                initSelectPopup(qData2,tv_question);
                break;
            case "三":
                rl15.setVisibility(View.VISIBLE);
                rl16.setVisibility(View.GONE);
                rb3.setChecked(true);
                initSelectPopup(qData3,tv_question);
                break;
            case "无异常":
                rl15.setVisibility(View.GONE);
                rl16.setVisibility(View.VISIBLE);
                rb10.setVisibility(View.VISIBLE);
                rb9.setVisibility(View.VISIBLE);
                rb4.setChecked(true);
                break;
        }
        if (!m_deal.equals("")){
            switch (m_deal){
                case "现场处理":
                    rb9.setChecked(true);
                    break;
                case "计划作业":
                    rb10.setChecked(true);
                    break;
            }
        }
    }

    private void iniData() {
        //insert
        Resources resources = getResources();
        String[] damageTypes = resources.getStringArray(damageType);
        String[] q1 = resources.getStringArray(R.array.warnType_1);
        String[] q2 = resources.getStringArray(R.array.warnType_2);
        String[] q3 = resources.getStringArray(R.array.warnType_3);
        String[] roadWays = resources.getStringArray(roadWay);
        String[] units = resources.getStringArray(R.array.unit);
        for (int i = 0; i < damageTypes.length; i++) {
            damageTypeData.add(damageTypes[i]);
        }
        for (int i = 0; i < q1.length; i++) {
            qData1.add(q1[i]);
        }
        for (int i = 0; i < q2.length; i++) {
            qData2.add(q2[i]);
        }
        for (int i = 0; i < q3.length; i++) {
            qData3.add(q3[i]);
        }
        for (int i = 0; i < roadWays.length; i++) {
            roadWayData.add(roadWays[i]);
        }
        for (int i = 0; i < units.length; i++) {
            unitData.add(units[i]);
        }
        isAlter = getIntent().getBooleanExtra("alter",false);
        //巡道id
        patrolID = getIntent().getStringExtra("ID");
        mPatrol = TrackManager.getPatrol(patrolID);
        if (isAlter) {
            mID = getIntent().getStringExtra("detailID");
            PatrolDetailInfoBean detailInfoBean = TrackManager.getAllDetailBean(mID);
            //损坏部位
            init_damageType = detailInfoBean.damageType;
            //预判级别
            sel_warnType = detailInfoBean.warnType;

            //发现问题情况描述
            init_context = detailInfoBean.context;
            //桩号
            init_potMeter = detailInfoBean.potMeter;
            init_pot = detailInfoBean.pot;
            init_endpotMeter = detailInfoBean.endpotMeter;
            init_endpotKm = detailInfoBean.endpotKm;
            //调查方向
            init_roadAway = detailInfoBean.roadWay;
            //预计工程量
            init_workload = detailInfoBean.workload;
            //现场处置情况
            m_deal = detailInfoBean.deal;

            //color
            sel_warnColor = detailInfoBean.warnColor;
            //workload
            init_workload = detailInfoBean.workload;
            //init_unit
            init_unit = detailInfoBean.workUnit;
            String pn = detailInfoBean.photoNumber;
            if (!pn.equals("")){
                String[] imgs = pn.split(",");
                List<ImageItem> lastImgs = new ArrayList<>();
                for (int i=0;i<imgs.length;i++){
                    ImageItem item = new ImageItem();
                    item.path = imgs[i];
                    lastImgs.add(item);
                }
                adapter.setImages(lastImgs);
            }
        }else{
            //损坏部位
            init_damageType = damageTypeData.get(0);
            //预判级别
            sel_warnType = "一";
            //color
            sel_warnColor = "";
            //调查方向
            init_roadAway = roadWayData.get(0);
            //单位
            init_unit = unitData.get(0);
            //初始化预判级别
            rg1.check(R.id.rb1);
            //
            rg3.check(R.id.rb9);
            //context
            init_context = qData1.get(0);
            //现场处置情况
            m_deal = "现场处理";
            //预计工程量
            init_workload = "";
            //pot
            init_potMeter = 0;
            init_pot = 0;
            init_endpotMeter = 0;
            init_endpotKm = 0;
        }
        init_patrolType = mPatrol.patrolType;
        init_weather = mPatrol.weather;
        init_date = mPatrol.roadDate;
        init_roader = mPatrol.roader;
        init_car = mPatrol.roadCar;
        init_line = mPatrol.roadLine;
        init_sm= mPatrol.startPotMeter;
        init_skm = mPatrol.startPotKm;
        init_em = mPatrol.endPotMeter;
        init_ekm = mPatrol.endPotKm;
    }
    /**
     * 初始化popup窗口
     */
    private void initSelectPopup(final List<String> mData, final TextView mTv) {
        ListView mLv = new ListView(this);
        // 设置适配器
        ArrayAdapter mAdapter = new ArrayAdapter<String>(this, R.layout.popup_text_item, mData);
        mLv.setAdapter(mAdapter);

        // 设置ListView点击事件监听
        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if(mData.get(position).equals("手动输入")){
                    rl12.setVisibility(View.VISIBLE);
                }else{
                    rl12.setVisibility(View.GONE);
                }
                String value = mData.get(position);
                mTv.setText(value);
                mPopup.dismiss();
            }
        });
        mPopup = new PopupWindow(mLv, mTv.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 取得popup窗口的背景图片
        Drawable drawable = ContextCompat.getDrawable(this, R.drawable.bg1);
        mPopup.setBackgroundDrawable(drawable);
        mPopup.setFocusable(true);
        mPopup.setOutsideTouchable(true);
        mPopup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                // 关闭popup窗口
                mPopup.dismiss();
            }
        });
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (position) {
            case IMAGE_ITEM_ADD:
                List<String> names = new ArrayList<>();
                names.add("拍照");
                names.add("相册");
                showDialog(new SelectDialog.SelectDialogListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        switch (position) {
                            case 0: // 直接调起相机
                                /**
                                 * 0.4.7 目前直接调起相机不支持裁剪，如果开启裁剪后不会返回图片，请注意，后续版本会解决
                                 *
                                 * 但是当前直接依赖的版本已经解决，考虑到版本改动很少，所以这次没有上传到远程仓库
                                 *
                                 * 如果实在有所需要，请直接下载源码引用。
                                 */
                                //打开选择,本次允许选择的数量
                                ImagePicker.getInstance().setSelectLimit(9 - selImageList.size());
                                Intent intent = new Intent(TrackDetailActivity.this, ImageGridActivity.class);
                                intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true); // 是否是直接打开相机
                                startActivityForResult(intent, REQUEST_CODE_SELECT);
                                break;
                            case 1:
                                //打开选择,本次允许选择的数量
                                ImagePicker.getInstance().setSelectLimit(9 - selImageList.size());
                                Intent intent1 = new Intent(TrackDetailActivity.this, ImageGridActivity.class);
                                /* 如果需要进入选择的时候显示已经选中的图片，
                                 * 详情请查看ImagePickerActivity
                                 * */
//                                intent1.putExtra(ImageGridActivity.EXTRAS_IMAGES,images);
                                startActivityForResult(intent1, REQUEST_CODE_SELECT);
                                break;
                            default:
                                break;
                        }

                    }
                }, names);


                break;
            default:
                //打开预览
                Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class);
                intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>) adapter.getImages());
                intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position);
                intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS, true);
                startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW);
                break;
        }
    }
    private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, List<String> names) {
        SelectDialog dialog = new SelectDialog(this, R.style
                .transparentFrameWindowStyle,
                listener, names);
        if (!this.isFinishing()) {
            dialog.show();
        }
        return dialog;
    }
    ArrayList<ImageItem> images = null;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            //添加图片返回
            if (data != null && requestCode == REQUEST_CODE_SELECT) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                if (images != null) {
                    selImageList.addAll(images);
                    adapter.setImages(selImageList);
                }
            }
        } else if (resultCode == ImagePicker.RESULT_CODE_BACK) {
            //预览图片返回
            if (data != null && requestCode == REQUEST_CODE_PREVIEW) {
                images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS);
                if (images != null) {
                    selImageList.clear();
                    selImageList.addAll(images);
                    adapter.setImages(selImageList);
                }
            }
        }
    }
    private void initImagePicker() {
        ImagePicker imagePicker = ImagePicker.getInstance();
        imagePicker.setImageLoader(new GlideImageLoader());   //设置图片加载器
        imagePicker.setShowCamera(true);                      //显示拍照按钮
//        imagePicker.setCrop(true);                           //允许裁剪（单选才有效）
        imagePicker.setMultiMode(true);
        imagePicker.setSaveRectangle(true);                   //是否按矩形区域保存
        imagePicker.setSelectLimit(9);              //选中数量限制
        imagePicker.setStyle(CropImageView.Style.RECTANGLE);  //裁剪框的形状
        imagePicker.setFocusWidth(800);                       //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.setFocusHeight(800);                      //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.setOutPutX(1000);                         //保存文件的宽度。单位像素
        imagePicker.setOutPutY(1000);                         //保存文件的高度。单位像素
    }
}
